const router = require('koa-router')();
const {db, QueryTypes,} = require("../../models/db");
const tool = require("../../tools/tool");


router.post('/contact/list', async ctx => {
    let tenantId = ctx.user.tenantId
    let body = ctx.request.body
    let formData = body.formData
    let excel = body.excel
    let name = formData.name
    let linkName = formData.linkName
    let linkMobile = formData.linkMobile
    let linkPhone = formData.linkPhone
    let address = formData.address
    let payType = formData.payType
    let categoryId = formData.categoryId
    let areaId = formData.areaId
    let sql = `select * from v_contact where tenantId=${tenantId} and isDel=0 `
    if(categoryId) {
        sql += ` and categoryId =${categoryId} `
    }
    if(areaId) {
        sql += ` and areaId =${areaId} `
    }
    if(name) {
        sql += ` and name like '%${name}%' `
    }
    if(linkName) {
        sql += ` and linkName like '%${linkName}%' `
    }
    if(linkMobile) {
        sql += ` and linkMobile like '%${linkMobile}%' `
    }
    if(linkPhone) {
        sql += ` and linkMobile like '%${linkPhone}%' `
    }
    if(address) {
        sql += ` and address like '%${address}%' `
    }
    if(payType) {
        sql += ` and payType='${payType}' `
    }
    sql += ' order by areaName desc,contactCatName desc, id desc '
    let list = await db.query(sql, {type: QueryTypes.SELECT})
    if(excel) {
        return tool.doExcelOut(ctx,list,)
    }

    return ctx.suc('', {
        list
    })

})




module.exports = router;